package com.dong.course.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.dong.course.dto.CourseDTO;
import com.dong.course.mapper.CourseMapper;
import com.dong.course.thrift.ServiceProvider;
import com.dong.thrift.user.UserInfo;
import com.dong.thrift.user.UserService;
import com.dong.thrift.user.dto.TeacherDTO;
import com.dong.thrift.user.dto.UserDTO;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Service(interfaceClass = ICourseService.class)
@Component
public class CourseServiceImpl implements  ICourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ServiceProvider serviceProvider;



    @Override
    public List<CourseDTO> courseList() {
        UserService.Client userService = serviceProvider.getUserService();
        List<CourseDTO> courseList = courseMapper.listCourse();
        for (CourseDTO courseDTO : courseList) {
            int userId = courseDTO.getTeacher().getId();
            try {
                UserInfo userInfo = userService.queryTeacherInfo(userId);
                courseDTO.setTeacher(TeacherDTO.convertUserInfo2TeacherDto(userInfo));

            } catch (TException e) {
                e.printStackTrace();
            }
        }
        return courseList;
    }
}
